package cn.zhiwen.xmn;

import java.util.ArrayList;

/**
 * 测试jvm 提供的jconsole 工具测试内存
 */
public class XmnTest {
    public static void main(String[] args) throws InterruptedException {
        try {
            fillHeap(1000);
        } catch (InterruptedException e) {
            System.out.println(e.getCause());
        }
        Thread.sleep(6000);
        //可以观察GC在什么时候有效
        System.gc();
        Thread.sleep(6000);
    }

    public static void fillHeap(Integer num) throws InterruptedException {
        ArrayList<OOMObject> oomObjects = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Thread.sleep(100);
            oomObjects.add(new OOMObject());
        }
        System.gc();
    }
    //内存占位符对象 一个对象向占64
    static class OOMObject{
        public byte[] placeholder = new byte[1024*64];
    }
}


